//--------------------get请求类型数据获取------------------------//

const http = require('http')
const querystring = require('querystring')

let app = http.createServer((req, res) => {
  let urlArray = req.url.split('?')
  req.query = {}
  if (urlArray && urlArray.length > 0) {
    if (urlArray[1]) {
      req.query = querystring.parse(urlArray[1])
    }
  }
  res.end(
    JSON.stringify(req.query)
  )
})

app.listen(8000, () => {
  console.log('running on 8000')
})

//-------------------------post请求类型数据获取----------------------//

const http = require('http')

let app = http.createServer((req, res) => {
  let postData = ''
  
  req.on('data', chunk => {
    postData += chunk.toString()
  })

  req.on('end', () => {
    if (postData) {
      res.setHeader('Content-type', 'application/json')
      res.end(postData)
    }
    console.log(JSON.parse(postData))
  })
  
})

app.listen(8000, () => {
  console.log('running on 8000')
})

//--------------------get/post合体-----------------------//

const url = require('url');
const http = require('http');
const server = http.createServer((req, res) => {
    if (req.method === 'GET') {
        let urlObj = url.parse(req.url, true);
        res.end(JSON.stringify(urlObj.query))
    } else if (req.method === 'POST') {
        let postData = '';
        req.on('data', chunk => {
        postData += chunk;
        })
        req.on('end', () => {
        console.log(postData)
        })
        res.end(JSON.stringify({
        data: '请求成功',
        code: 0
        }))
    }
})
server.listen(3000, () => {
    console.log('监听3000端⼝')
    console.log(1111)
    console.log(22)
})